#ifndef _GISUSDB_HXX_
#define _GISUSDB_HXX_

#include "mytypes.h"
#include "mydll.h"
#include "mymutex.hxx"
#include <vector>
#include <task.hxx>

class GisusDBImpl;
class DB;
class GisusSWFileSet;
class FileLog;
class Task;
class User;
class Keyword;
class GisusDB;
class DBTableLock
{
public:
	DBTableLock(GisusDB* gisusdb1, const string& tablename, FileLog* filelog = NULL);
	~DBTableLock();
private:
	GisusDB* gisusdb;
	FileLog* _filelog;
};
class GisusDB
{
 public:
  GisusDB(DB *db, FileLog *filelog=0);
  ~GisusDB();
  bool Connect();
  bool Disconnect();
  User* Login(const string &user, const string &pwd);
  int SaveTask(const Task* task);
  bool UpdateTask(const Task* task);
  bool MarkStatus(const Task* task, bool isZombie);
  int NewZombie(const Task* task);
  std::vector<Task*> GetZSeeds(Task::TaskStatus status = Task::ZOMBIE_SEED);
  std::vector<Task*> LoadZombies(const Task* seed, int num);
  bool UpdateZombie(const Task* task);
  int AddTaskKeyword(int taskId, int keywordId);
  
  Task* GetTaskByCjh(const string& chj, const User* user);
  Task* GetNeighbor(const User* user, int curTaskId, bool next=true);
  std::vector<Task*> GetPTasks(const User* user, Task::TaskStatus status = Task::CREATED);
  bool DeleteTask(const string& cjh);
  bool MarkPaid(const string& cjh);
  bool Restore(const string& cjh);
  bool RestoreBEndData(const User* user);
  bool LockDBTable(const string& table);
  bool UnlockDBTables(void);

 private:
  // Copy constructor is private to FORCE the user to instantiate 
  // a new object with 'new' instead of static allocation.
  // This is because we cannot copy this object so easily 
  // mainly because of internal objects.
  GisusDB(const GisusDB &model);

  Mutex dbInUse;

   GisusDBImpl *_impl;  
};

#endif // _GISUSDB_HXX_
